var res = /[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/;
var app = new Vue({
    el: ".box",
    data: {
        usernameRules: [{
            validate: (val) => !!val,
            message: '必须填写邮箱'
        }, {
            validate: (val) => res.test(val),
            message: '请输入正确的邮箱'
        }],
        passwordRules: [{
            validate: (val) => !!val,
            message: '必须填写昵称'
        },],
        yan: [{
            validate: (val) => !!val,
            message: '必须填写验证码'
        }, {
            validate: (val) => val.length === 6 && val - 0,
            message: '验证码必须是6位数字'
        }],
        validateForm: {
            email: '',
            name: '',
            yan: ''
        },
        openAlert: false,
        openSimple: false,
        msg: '',
        yan_text: '获取验证码',
        nums: 60,
        yan_diabled: true,
        code_num: ''
    },
    methods: {
        submit() {
            this.$refs.form.validate().then((result) => {
                if(result) {
                    this.openAlert = true;
                };
            });
        }, set_yan() {
            if (this.validateForm.email && this.validateForm.name) {
                this.yan_diabled = false;
            } else {
                this.yan_diabled = true;
            };
        }, get_yan() {
            var _this = this;
            var tiemr = null;
            clearInterval(tiemr);
            if (!this.yan_diabled) {
                get_yans({
                    email: _this.validateForm.email,
                    title: '爱尚博客订阅验证码',
                    cont: _this.validateForm.name,
                    head: 20000
                });
                _this.yan_text = _this.nums + '秒后重新获取';
                this.yan_diabled = true;
                tiemr = setInterval(function () {
                    _this.nums--;
                    _this.yan_text = _this.nums + '秒后重新获取';
                    if (_this.nums === 0) {
                        _this.nums = 60;
                        _this.yan_diabled = false;
                        _this.yan_text = '获取验证码';
                        clearInterval(tiemr);
                    };
                }, 1000);
            };
        }, closeAlertDialog() {
            this.openAlert = !this.openAlert;
        }, suss() {
            this.openAlert = !this.openAlert;
            Subscribes(this.validateForm);
        }, closeSimpleDialog() {
            this.openSimple = !this.openSimple;
        }
    }
});

function Subscribes(obj) {
    var data = {};
    if (!obj.email) {
        return false;
    } else {
        data.email = obj.email;
    };
    if (!obj.name) {
        return false;
    } else {
        data.name = obj.name;
    };
    if (app.code_num != md5(app.validateForm.yan) || !app.code_num) {
        app.openSimple = !app.openSimple;
        app.msg = "验证码错误！如果输入正确验证码并提示错误，请联系管理员。";
        return false;
    };
    layer.load(2);
    axios({
        method: "POST",
        baseURL: "../api/Subscribe.php",
        headers: {
            "Content-type": "application/json; charset=utf-8"
        },
        data: JSON.stringify(data)
    }).then((res) => {
        layer.closeAll('loading');
        app.openSimple = !app.openSimple;
        app.msg = res.data.msg;
    }).
        catch((res) => {
            console.log(res);
        })
};

function get_yans(obj) {
    layer.load(2);
    axios({
        method: "POST",
        baseURL: "../api/get_code.php",
        headers: {
            "Content-type": "application/json; charset=utf-8"
        },
        data: JSON.stringify(obj)
    }).then((res) => {
        layer.closeAll('loading');
        var tiemr = null;
        clearTimeout(tiemr);
		app.openSimple = !app.openSimple;
        app.msg = "发送验证码成功！";
        app.code_num = res.data.msg;
        tiemr = setTimeout(function () {
            app.code_num = '';
        }, 100000);
    }).
        catch((res) => {
            console.log(res);
        })
};

function get_user_info() {
    var data = {
        href: window.location.href,
        title: document.title,
        timer: 5000,
        source: document.referrer
    };
    axios({
        method: "POST",
        //类型
        baseURL: '../api/log.php',
        headers: {
            "Content-type": "application/json; charset=utf-8"
        },
        data: JSON.stringify(data)
    }).then(function (res) { }).
        catch(function (err) {
            console.log(err)
        })
};
setTimeout(function () {
    get_user_info()
}, 5000);